package com.homegoods.orderinfodetail.presenter;

import android.databinding.ObservableField;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.bean.AddAddressParams;
import com.homegoods.orderinfodetail.event.EditAddressEvent;
import com.homegoods.orderinfodetail.model.AddressModel;
import com.homegoods.orderinfodetail.ui.view.IAddAddressView;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.ToastUtil;

import org.json.JSONObject;

import io.reactivex.functions.Consumer;

/**
 * @author QiQi
 */
public class AddNewAddressPresenter extends BasePresenter<IAddAddressView> {
    private static final String TAG = "AddAddressPresenterTag";

    private boolean isEditMode = false;
    private int index = 0;
    private String postAreaSid;
    private AddressModel model;   //del shibo.zheng 19/07/01

    public void setPostAreaSid(String sid) {
        this.postAreaSid = sid;
    }

    /**
     * 收货人
     */
    public final ObservableField<String> name = new ObservableField<>();
    /**
     * 手机号码
     */
    public final ObservableField<String> phone = new ObservableField<>();
    /**
     * 所在地区
     */
    public final ObservableField<String> area = new ObservableField<>();
    /**
     * 详细地址
     */
    public final ObservableField<String> address = new ObservableField<>();

    public AddNewAddressPresenter(IAddAddressView view) {
        super(view);
    }

    public void setAddressModel(int index, AddressModel addressModel) {
        if (addressModel == null) {
            return;
        }
        model = addressModel;
        isEditMode = true;
        this.index = index;

        address.set(addressModel.getTitle());
        phone.set(addressModel.getMPhone());
        name.set(addressModel.getContacts());
        area.set(addressModel.getAreaSidTree());
    }

    /**
     * 保存按钮点击事件
     * @param view
     */
    public void onSaveClick(View view) {
        AddAddressParams params = new AddAddressParams();
        if (TextUtils.isEmpty(address.get())) {
            ToastUtil.showSnackbar(view, "请填写详细地址");
            return;
        } else if (TextUtils.isEmpty(phone.get())) {
            ToastUtil.showSnackbar(view, "请填写手机号");
            return;
        } else if (TextUtils.isEmpty(name.get())) {
            ToastUtil.showSnackbar(view, "请填写收货人姓名");
            return;
        } else if (TextUtils.isEmpty(area.get())) {
            ToastUtil.showSnackbar(view, "请选择收货人所在地区");
            return;
        }

        if (model == null || !isEditMode) {
            model = new AddressModel();
            model.setSid("");
        }
        model.setTitle(address.get());
        model.setMPhone(phone.get());
        model.setAreaSidTree(area.get());
        model.setContacts(name.get());
        model.setAreaSid(postAreaSid);
        model.setIsDefault(mView.isCheck() ? 1 : 0);

        params.setAreaId(postAreaSid);
        params.setIsDefault(mView.isCheck());
        params.setDetailAddress(address.get());
        params.setName(name.get());
        params.setPhone(phone.get());
        params.setAreaName("nihaohao");

        OrderInfoApiUtils.setArea(params, jsonObject -> {
            Log.d(TAG, "onSaveClick: 数据请求成功");
            //保存地址成功，发送消息到我的收货地址界面，通知界面刷新
            RxBus.getDefault().post(new EditAddressEvent(isEditMode, index, model));
            mView.submit();
        }, throwable -> {
            Log.d(TAG, "onSaveClick: throwable == " + throwable.toString());
        });


    }
}